package com.xurui.kotlindelegate

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.os.Bundle

inline fun <reified T> Context.start(args: Bundle? = null) {
    val intent = Intent(this, T::class.java)
    if (args != null) {
        intent.putExtras(args)
    }
    if (this !is Activity) {
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    }
    this.startActivity(intent)
}

fun Context?.getActivity(): Activity? {
    this ?: return null
    return when (this) {
        !is ContextWrapper -> null
        is Activity -> this
        else -> this.baseContext.getActivity()
    }
}